﻿var express = require('express');
var router = express.Router();

var msgHandlerAndRedirect = function(result, req, res, nextUrlIfErr, nextUrlIfInf) {
    var msg;
    if (result.err) {
        msg = [result.err.toString("utf8")];
        if (!!req.session.dangerAlerts) {
            msg = msg.concat(req.session.dangerAlerts.msg);
            req.session.dangerAlerts.msg.length = 0;
            //delete req.session.dangerAlerts;
        }
        req.session.dangerAlerts = { msg: msg };
        res.redirect(nextUrlIfErr(result, req));
    } else if (result.inf) {
        msg = [result.inf.toString("utf8")];
        if (!!req.session.successAlerts) {
            msg = msg.concat(req.session.successAlerts.msg);
            req.session.successAlerts.msg.length = 0;
            //delete req.session.successAlerts;
        }
        req.session.successAlerts = { msg: msg };
        res.redirect(nextUrlIfInf(result, req));
    }
};

router.post('/delete', function(req, res) {
    req.pool.acquire(function(err, conn) {
        conn.call('ParticipantRole.deleteRole', req.body, function(err, result) {
            req.pool.release(conn);
            msgHandlerAndRedirect(
                result, req, res, 
                function(result, req) {
                    return 'back';
                },
                function(result, req) {
                    var referrer = req.header('REFERER');
                    var arr = referrer.split('/roles/page');
                    referrer = parseInt(arr[arr.length - 1]);
                    if (isNaN(referrer) || referrer < 0 || referrer > (result.lastPage - 1)) {
                        referrer = (result.lastPage - 1);
                    }
                    return '/roles/page' + referrer;
                });
        });
    });
});

router.post('/create', function(req, res) {
    req.pool.acquire(function(err, conn) {
        conn.call('ParticipantRole.createRole', req.body, function(err, result) {
            req.pool.release(conn);
            msgHandlerAndRedirect(
                result, req, res, 
                function(result, req) {
                    return 'back';
                },
                function(result, req) {
                    return '/roles/page' + (result.lastPage - 1);
            });
        });
    });
});

router.post('/update', function(req, res) {
    req.pool.acquire(function(err, conn) {
        conn.call('ParticipantRole.updateRole', req.body, function(err, result) {
            req.pool.release(conn);
            msgHandlerAndRedirect(
                result, req, res, 
                function(result, req) {
                    return 'back';
                },
                function(result, req) {
                    return 'back';
                });
        });
    });
});

router.get('/*', function(req, res, next) {
    if (!!req.session.dangerAlerts) {
        req.model.dangerAlerts = req.model.dangerAlerts.concat(req.session.dangerAlerts.msg);
        req.session.dangerAlerts.msg.length = 0;
        //delete req.session.dangerAlerts;
    }
    if (!!req.session.successAlerts) {
        req.model.successAlerts = req.model.successAlerts.concat(req.session.successAlerts.msg);
        req.session.successAlerts.msg.length = 0;
        //delete req.session.successAlerts;
    }
    if (!!req.session.infoAlerts) {
        req.model.infoAlerts = req.model.infoAlerts.concat(req.session.infoAlerts.msg);
        req.session.infoAlerts.msg.length = 0;
        //delete req.session.infoAlerts;
    }
    if (!!req.session.warningAlerts) {
        req.model.warningAlerts = req.model.warningAlerts.concat(req.session.warningAlerts.msg);
        req.session.warningAlerts.msg.length = 0;
        //delete req.session.warningAlerts;
    }
    next();
});

router.get('/*', function(req, res, next) {
    req.pool.acquire(function(err, conn) {
        conn.call('ParticipantRole.getRolesPagesCount', {}, function(err, result) {
            req.model.pages = result.count;
            req.model.entity = 'role';
            req.model.relative_path = '/roles';
            req.model.title = 'Roles';
            req.pool.release(conn);
            next();
        });
    });
});

var renderPage = function(req, res) {
    req.pool.acquire(function(err, conn) {
        conn.call('ParticipantRole.getRolesWithInfo', { page: req.model.page }, function(err, result) {
            req.pool.release(conn);
            req.model.data = result;
            res.render('roles', req.model);
        });
    });
};

router.get('/', function(req, res) {
    req.model.page = 0;
    renderPage(req, res);
});

router.get('/page:id', function(req, res) {
    req.model.page = parseInt(req.params.id);
    if (isNaN(req.model.page)) req.model.page = 0;
    if (req.model.page < 0) req.model.page = 0;
    renderPage(req, res);
});

module.exports = router;